﻿package com 
{
	import flash.utils.ByteArray;
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class BinaryEncrypt 
	{
		public function BinaryEncrypt():void
		{
			
		}
		
		//这个就是解密函数
		public static function unEncry_swf(swf:ByteArray,key:String):ByteArray
		{
			var key_ar:Array = Conversion_key(key);
			var num:uint = key_ar.length;
            return recover_swf_header(remove_random_bytes(un_split_join_swf(swf, key_ar), num));
		}
		
		//验证KEY的格式是否合法
		public static function check_key(key:String):Boolean
        {
			key = trim(key);
            var r:RegExp = /^(\d-)+\d$/;
            return r.test(key);
        }

		
		private static function Conversion_key(key:String):Array
		{
			key = trim(key);
			var ar:Array = key.split("-");
			
			return ar;
		}
		
        private static function trim(key:String):String
        {
            var l:RegExp = /^-+/;
            var r:RegExp = /-+$/;

            key = key.replace(l, "");
            key = key.replace(r, "");
            return key;
        }
		
		//恢复swf文件的文件头
        private static function recover_swf_header(swf:ByteArray):ByteArray
        {
            swf[0] = 67;
            swf[1] = 87;
            swf[2] = 83;

            return swf;
        }
		
        //在字节数组两边删除一定数量的垃圾字节		
		private static function remove_random_bytes(swf:ByteArray, num:int):ByteArray
        {
			var swf_l:uint = swf.length;

			var swf_unencrypt:ByteArray = new ByteArray();
			swf_unencrypt.length = swf_l - num * 2;
			swf_unencrypt.position = 0;
			swf.position = num;
			swf.readBytes(swf_unencrypt, 0, swf_unencrypt.length);

            return swf_unencrypt;
        }
		
		private static function un_split_join_swf(swf:ByteArray, ar:Array):ByteArray
        {
            //要分割的块数
            var c:uint = ar.length;
            //字节总数
            var swf_l:uint = swf.length;

            //拆分时尾部一小块的大小
            var swf_ed:uint = swf_l % c;

            //被拆分后每块的大小
            var swf_c:uint = swf_l / c;


            //得到拆分的每一块
            var al:Array = new Array(c);

            for (var i:uint = 0; i < c; i++)
            {
                var tmp_byte:ByteArray = new ByteArray();
				tmp_byte.length = swf_c;
				swf.position = i * swf_c;
				swf.readBytes(tmp_byte, 0, tmp_byte.length);
                al[ar[i]] = tmp_byte;
            }
			
            //最后一小块
            var last:ByteArray = new ByteArray();
			last.length = swf_ed;
			swf.position = swf_l - swf_ed;
			swf.readBytes(last, 0, swf_ed);

            
            //将字节块归位
            var swf_unencrypt:ByteArray = new ByteArray();
			swf_unencrypt.length = swf_l;
			
          
            for (var i:uint = 0; i < c; i++)
            {
                var tmp_byte:ByteArray = al[i];
				tmp_byte.position = 0;
				tmp_byte.readBytes(swf_unencrypt, i * swf_c, swf_c);
            }
			last.position = 0;
            last.readBytes(swf_unencrypt, swf_l - swf_ed, swf_ed);

            return swf_unencrypt;
        }
	}
	
}